﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WorldBankBBS.Logic;

namespace WorldBankBBS.Subboards
{
    public class SubboardsEvents
    {
        private Session _session = null;
        public SubboardsEvents(Session session)
        {
            _session = session;
        }
        public event EventHandler<Session> PreCreateMessage;
        public event EventHandler<Session> CreateMessage;
        public event EventHandler<Session> PostCreateMessage;

        public event EventHandler<Session> PreDeleteMessage;
        public event EventHandler<Session> DeleteMessage;
        public event EventHandler<Session> PostDeleteMessage;

        public event EventHandler<Session> ReadMessage;
        public event EventHandler<Session> PostMessageRead;

        public event EventHandler<Session> EnterMessageSection;

        public event EventHandler<Session> ListMessages;
        public event EventHandler<Session> ListUnreadMessages;

        public event EventHandler<Session> ListSubboards;

        public event EventHandler<Session> EnterSubboard;

        public event EventHandler<Session> ListThreads;
        public event EventHandler<Session> ChangeThread;

        public event EventHandler<Session> PreCreateThread;
        public event EventHandler<Session> CreateThread;
        public event EventHandler<Session> PostCreateThread;

        public event EventHandler<Session> ReadNextMessage;

        public event EventHandler<Session> NewScan;

        public void InvokePreCreateThread()
        {
            if (PreCreateThread != null)
            {
                PreCreateThread(this, _session);
            }
        }

        public void InvokeCreateThread()
        {
            if (CreateThread != null)
            {
                CreateThread(this, _session);
            }
        }

        public void InvokePostCreateThread()
        {
            if (PostCreateThread != null)
            {
                PostCreateThread(this, _session);
            }
        }

        public void InvokeListSubboards()
        {
            if (ListSubboards != null) ListSubboards(this, _session);
        }

        public void InvokeEnterSubboard(Guid uid)
        {
            if (EnterSubboard != null)
            {
                if (_session.ContainsProperty("CurrentSubboardUID") && uid != (_session["CurrentSubboardUID"] as Guid? ?? Guid.Empty))
                {
                    _session["CurrentSubboardUID"] = (Guid?)uid;
                    _session["CurrentSubboardChanged"] = true;
                    EnterSubboard(this, _session);
                }
                else
                {
                    _session["CurrentSubboardChanged"] = false;
                }
            }
        }

        public void InvokeListThreads()
        {
            if (ListThreads != null && _session.ContainsProperty("CurrentSubboardUID"))
            {
                var uid = _session["CurrentSubboardUID"] as Guid? ?? Guid.Empty;
                InvokeEnterSubboard(uid);

                ListThreads(this, _session);
            }
        }

        public void InvokeEnterMessageSection()
        {
            if (EnterMessageSection != null) EnterMessageSection(this, _session);
        }

        public void InvokeListMessages()
        {
            if (ListMessages != null) ListMessages(this, _session);
        }

        public void InvokeListUnreadMessages()
        {
            if (ListUnreadMessages != null) ListUnreadMessages(this, _session);
        }

        public void InvokePreCreateMessage()
        {
            if (PreCreateMessage != null)
            {
                PreCreateMessage(this, _session);
            }
        }

        public void InvokeCreateMessage(Guid uid)
        {
            if (CreateMessage != null)
            {
                _session["ReplyToSubboardMessageUID"] = uid;
                CreateMessage(this, _session);
            }
        }

        public void InvokePostMessageRead()
        {
            if (PostMessageRead != null)
            {
                PostMessageRead(this, _session);
            }
        }

        internal void InvokeReadMessage(Guid uid)
        {
            if (ReadMessage != null)
            {
                _session["SubboardMessageUID"] = uid;
                ReadMessage(this, _session);
            }
        }

        public void InvokePostCreateMessage()
        {
            if (PostCreateMessage != null)
            {
                PostCreateMessage(this, _session);
            }
        }

        public void InvokePreDeleteMessage()
        {
            if (PreDeleteMessage != null)
            {
                PreDeleteMessage(this, _session);
            }
        }

        public void InvokeDeleteMessage(Guid uid)
        {
            if (DeleteMessage != null)
            {
                _session["CurrentMessageUID"] = uid;
                DeleteMessage(this, _session);
            }
        }

        public void InvokePostDeleteMessage()
        {
            if (PostDeleteMessage != null)
            {
                PostDeleteMessage(this, _session);
            }
        }

        public void InvokeChangeThread(Guid uid)
        {
            _session["CurrentThreadUID"] = uid;
            if (ChangeThread != null)
            {
                ChangeThread(this, _session);
            }
        }

        public void InvokeReadNextMessage()
        {
            if (ReadNextMessage != null)
            {
                ReadNextMessage(this, _session);
            }
        }

        public void InvokeNewScan()
        {
            if (NewScan != null)
            {
                NewScan(this, _session);
            }
        }
    }
}
